import request from '@/service/request';
import type {
  AccountLogin,
  AccountRegister,
  ChangePassword,
  EditName,
  EditNewAccount,
  RetrievePassword,
  VerificationCode,
  VerifiMobile,
} from './type';

// 注册请求
export function userRegesiterDto(data: {
  code: string;
  department: string;
  email: string;
  name: string;
  orgName: string;
  phone: string;
  position: string;
}) {
  return request.post<AccountRegister>({
    url: '/user/register',
    data,
    interceptors: {
      responseInterceptor(res: any): AccountRegister {
        return {
          data: res,
        };
      },
    },
  });
}

// 账号密码登录请求
export function requestAccountLogin(data: { password: string; loginUser: string }) {
  return request.post<AccountLogin>({
    url: '/user/Login',
    data,
    interceptors: {
      responseInterceptor(res: any): AccountLogin {
        return res?.data?.token;
      },
    },
  });
}
// 手机验证码登录请求
export function requestPhoneLogin(params: { phone: string; code: string }) {
  return request.post<AccountLogin>({
    url: '/user/loginByPhoneCode',
    params,
    interceptors: {
      responseInterceptor(res: any): AccountLogin {
        return res?.data?.token;
      },
    },
  });
}

// 找回密码请求
export function requestRetrievePassword(params: { code: string; information: string; password: string }) {
  return request.post<RetrievePassword>({
    url: '/user/searchPwd',
    params,
    interceptors: {
      responseInterceptor(res: any): RetrievePassword {
        return {
          data: res.data,
        };
      },
    },
  });
}

// 获取用户信息
export function requestUserInfo() {
  return request.post({
    url: '/user/showUserInfo',
  });
}

// 手机发送验证码
export function verificationCode(params: { phone: number }) {
  return request.post<VerificationCode>({
    url: '/user/sendPhoneCode',
    params,
    interceptors: {
      responseInterceptor(res: any): VerificationCode {
        return {
          ...res.data,
        };
      },
    },
  });
}
// 邮箱发送验证码
export function emailVerificationCode(params: { email: string }) {
  return request.post<VerificationCode>({
    url: '/user/sendEmailCode',
    params,
    interceptors: {
      responseInterceptor(res: any): VerificationCode {
        return {
          ...res.data,
        };
      },
    },
  });
}

// 个人账户信息修改

// 修改密码
export function modifyPassword(params: { newPassword: string; oldPassword: string }) {
  return request.post<ChangePassword>({
    url: '/user/modifyPassword',
    params,
    interceptors: {
      responseInterceptor(res: any): ChangePassword {
        return {
          ...res,
        };
      },
    },
  });
}

// 判断用户输入的邮箱或者手机验证码是否正确
export function verifiCode(params: { code: string; inputContent: string }) {
  return request.post<VerifiMobile>({
    url: '/user/verifiMobile',
    params,
    interceptors: {
      responseInterceptor(res: any): VerifiMobile {
        return {
          ...res,
        };
      },
    },
  });
}

// 修改手机号或邮箱
export function editMobile(params: { code: string; inputContent: string }) {
  return request.post<EditNewAccount>({
    url: '/user/editMobile',
    params,
    interceptors: {
      responseInterceptor(res: any): EditNewAccount {
        return {
          ...res,
        };
      },
    },
  });
}

// 修改用户名称
export function editUserName(params: { id: string; name: string }) {
  return request.post<EditName>({
    url: '/user/editName',
    params,
    interceptors: {
      responseInterceptor(res: any): EditName {
        return {
          ...res,
        };
      },
    },
  });
}
